﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;

/// <summary>
/// 租户路由 /api/{version}/[controler]/[action]
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class TenantRouteAttribute : RouteAttribute, IApiDescriptionGroupNameProvider
{

    /// <summary>
    /// 分组名称,是来实现接口 IApiDescriptionGroupNameProvider
    /// </summary>
    public string GroupName { get; set; }


    /// <summary>
    /// 自定义版本+路由构造函数，继承基类路由
    /// </summary>
    /// <param name="moduleName">模块名称</param>
    /// <param name="version"></param>
    public TenantRouteAttribute(string moduleName = "admin", string version = "v1")
        : base(GetRouteTemplate(version, moduleName))
    {
        GroupName = moduleName.ToString();
    }


    private static string GetRouteTemplate(string version, string moduleName = "admin")
    {
        //  return "/{__tenant__:required}/" + moduleName.ToString().Camelize() + "/api/" + version + "/[controller]/" + actionName;
        return $"/{{__tenant__:required:length(4,10)}}/{moduleName}/api/{version}/[controller]/[action]";
    }
}
